home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Xconq 7.1.0 / src / xconq-7.1.0 / x11 / xprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  12.9 KB  |  475 lines  |  [TEXT/R*ch]

  1. /* Print dialog for the X11 interface to Xconq.
  2.    Copyright (C) 1994, 1995 Massimo Campostrini & Stanley T. Shebs.
  3.  
  4. Xconq is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.  See the file COPYING.  */
  8.  
  9. /* This is a print setup dialog, originally written by
  10.    Massimo Campostrini <campo@sunthpi3.difi.unipi.it> in May 1994. */
  11.  
  12. #include "conq.h"
  13. #include "xconq.h"
  14.  
  15. typedef struct a_widgetId {
  16.   String      name;
  17.   int         newline;
  18. } WidgetId;
  19.  
  20. typedef struct a_choice {
  21.   int number;
  22.   String legends[5];
  23. } Choice;
  24.  
  25. Choice choices[N_CHOICE] = {
  26.   {3, 
  27.      {"no names",
  28.       "interesting names",
  29.       "all names",
  30.       NULL, NULL}
  31.  }
  32. };
  33.  
  34. static void print_ok PARAMS ((Side *side));
  35. static void print_skip PARAMS ((Side *side));
  36. static void open_print_help PARAMS ((Side *side));
  37. static int w_comm_ind PARAMS ((Side *side, Widget w));
  38. static void update_values PARAMS ((Side *side));
  39. static void read_all PARAMS ((Side *side));
  40.  
  41. static void handle_do_print_dialog PARAMS ((Widget w, XEvent *event,
  42.                       String *params,
  43.                       Cardinal *num_params));
  44.  
  45. static void print_setup_button_callback PARAMS ((Widget w,
  46.                          XtPointer client_data,
  47.                          XtPointer call_data));
  48. static void metric_convert_callback PARAMS ((Widget w,
  49.                          XtPointer client_data,
  50.                          XtPointer call_data));
  51. static void done_print_help_callback PARAMS ((Widget w,
  52.                           XtPointer client_data,
  53.                           XtPointer call_data));
  54. static void handle_done_print_help PARAMS ((Widget w, XEvent *event,
  55.                         String *params,
  56.                         Cardinal *num_params));
  57.  
  58. static XtActionsRec xprint_actions[] = {
  59.   { "DoPrintDialog", handle_do_print_dialog },
  60.   { "DonePrintHelp", handle_done_print_help },
  61. };
  62.  
  63. WidgetId w_table[N_WIDGET] = {
  64.   {"help",         1 },
  65.   {"print",        0 },
  66.   {"quit",         0 },
  67.   {"names",        1 },
  68.   {"coord",        0 },
  69.   {"dither",       0 },
  70.   {"double",       1 },
  71.   {"features",     0 },
  72.   {"summary",      0 },
  73.   {"metric",       0 },
  74.   {"cellSize",     1 },
  75.   {"gridSize",     0 },
  76.   {"borderWidth",  1 },
  77.   {"connWidth",    0 },
  78.   {"pageWidth",    1 },
  79.   {"pageHeight",   0 },
  80.   {"topMargin",    1 },
  81.   {"bottomMargin", 0 },
  82.   {"leftMargin",   1 },
  83.   {"rightMargin",  0 },
  84.   {"terrainGray",  1 },
  85.   {"enemyGray",    0 },
  86. };
  87.  
  88. static String helpText =
  89. "Modify values, then press \"print\" (or \"quit\" to skip printing).\n\
  90.  \n\
  91. Multiple choices (click to cycle)\n\
  92. o    print unit names:  no names, interesting names, all names.\n\
  93.  \n\
  94. Binary options (dark background = set)\n\
  95. o    coord:    print coordinates of map corners\n\
  96. o    dither:   use dithering to print terrain gray\n\
  97. o    double:   map a pixel into 2x2 pixels wile dithering\n\
  98. o    features: print geographical features\n\
  99. o    summary:  print summary of units in cell\n\
  100. o    metric:   print distances in centimetres (otherwise in inches)\n\
  101.  \n\
  102. Numerical options (accept floating-point values)\n\
  103. o    cell size:  printed size of hexagonal cells\n\
  104. o    grid thick.:  thickness of cell grid (0 to suppress)\n\
  105. o    border/conn thick.:  thickness of borders and connections\n\
  106. o    page width/height:  dimension of sheets to print on\n\
  107. o    top/bottom/left/right margin:  margins of the region where\n\
  108. cells are printed; labels and titles are printed in the margins\n\
  109. o    border/connection thickness\n\
  110. o    terrain gray:  from 0 (black) to 1 (white)\n\
  111. o    enemy gray:    from 0 (black) to 1 (white), [not implemented]";
  112.  
  113. int
  114. popup_print_setup_dialog(side)
  115. Side *side;
  116. {
  117.     int i;
  118.     Widget w_form, w_left, w_up, w_oldup;
  119.     WidgetClass class;
  120.     
  121.     XtAppAddActions(thisapp, xprint_actions, XtNumber(xprint_actions));
  122.     
  123.     if (!side->ui->print_shell) {
  124.     side->ui->print_shell =
  125.       XtVaCreatePopupShell("PrintSetup", topLevelShellWidgetClass,
  126.                    side->ui->shell, NULL);
  127.   
  128.     w_form = XtVaCreateManagedWidget ("form", formWidgetClass, side->ui->print_shell, NULL);
  129.     w_left = w_up = w_oldup = NULL;
  130.   
  131.     for (i = 0; i < N_WIDGET; i++) {
  132.         if (i < N_BUTTON)
  133.           class = commandWidgetClass;
  134.         else if (i < N_BUTTON + N_TOGGLE)
  135.           class = toggleWidgetClass;
  136.         else
  137.           class = dialogWidgetClass;
  138.     
  139.         side->ui->print_cmds[i] =
  140.           XtVaCreateManagedWidget (w_table[i].name, class, w_form,
  141.                        NULL);
  142.         if (w_table[i].newline) {
  143.         w_oldup = w_up;
  144.         w_up = side->ui->print_cmds[i];
  145.         w_left = NULL;
  146.         }
  147.         XtVaSetValues(side->ui->print_cmds[i],
  148.               XtNfromVert, w_oldup,
  149.               XtNfromHoriz, w_left,
  150.               NULL);
  151.         w_left = side->ui->print_cmds[i];
  152.     
  153.         if (strcmp(w_table[i].name, "metric") == 0) {
  154.         XtAddCallback (side->ui->print_cmds[i], XtNcallback,
  155.                    metric_convert_callback, NULL);
  156.         side->ui->i_metric = i;
  157.         }
  158.         /* yes, we really need to cast commandWidgetClass into WidgetClass */
  159.         if (class == (WidgetClass) commandWidgetClass) {
  160.         XtAddCallback (side->ui->print_cmds[i], XtNcallback,
  161.                    print_setup_button_callback, NULL);
  162.         }
  163.     }
  164.   
  165.     XtRealizeWidget (side->ui->print_shell);
  166.     }    
  167.     
  168.     /* unelegant... */
  169.     side->ui->choi[ 0] = side->ui->ps_pp->names;
  170.     side->ui->flag[ 0] = side->ui->ps_pp->corner_coord;
  171.     side->ui->flag[ 1] = side->ui->ps_pp->terrain_dither;
  172.     side->ui->flag[ 2] = side->ui->ps_pp->terrain_double;
  173.     side->ui->flag[ 3] = side->ui->ps_pp->features;
  174.     side->ui->flag[ 4] = side->ui->ps_pp->cell_summary;
  175.     side->ui->flag[ 5] = side->ui->ps_pp->cm;
  176.     side->ui->parm[ 0] = side->ui->ps_pp->cell_size;
  177.     side->ui->parm[ 1] = side->ui->ps_pp->cell_grid_width;
  178.     side->ui->parm[ 2] = side->ui->ps_pp->border_width;
  179.     side->ui->parm[ 3] = side->ui->ps_pp->connection_width;
  180.     side->ui->parm[ 4] = side->ui->ps_pp->page_width;
  181.     side->ui->parm[ 5] = side->ui->ps_pp->page_height;
  182.     side->ui->parm[ 6] = side->ui->ps_pp->top_margin;
  183.     side->ui->parm[ 7] = side->ui->ps_pp->bottom_margin;
  184.     side->ui->parm[ 8] = side->ui->ps_pp->left_margin;
  185.     side->ui->parm[ 9] = side->ui->ps_pp->right_margin;
  186.     side->ui->parm[10] = side->ui->ps_pp->terrain_gray;
  187.     side->ui->parm[11] = side->ui->ps_pp->enemy_gray;
  188.     update_values(side);
  189.     
  190.     XtPopup(side->ui->print_shell, XtGrabNone);
  191.     
  192.     return 0;
  193. }
  194.  
  195. static void
  196. print_ok(side)
  197. Side *side;
  198. {
  199.     double conv;
  200.  
  201.     /* unelegant... */
  202.     side->ui->ps_pp->names =        side->ui->choi[ 0];
  203.     side->ui->ps_pp->corner_coord =        side->ui->flag[ 0];
  204.     side->ui->ps_pp->terrain_dither =    side->ui->flag[ 1];
  205.     side->ui->ps_pp->terrain_double =    side->ui->flag[ 2];
  206.     side->ui->ps_pp->features =        side->ui->flag[ 3];
  207.     side->ui->ps_pp->cell_summary =        side->ui->flag[ 4];
  208.     side->ui->ps_pp->cm =            side->ui->flag[ 5];
  209.     side->ui->ps_pp->cell_size =        side->ui->parm[ 0];
  210.     side->ui->ps_pp->cell_grid_width =    side->ui->parm[ 1];
  211.     side->ui->ps_pp->border_width =        side->ui->parm[ 2];
  212.     side->ui->ps_pp->connection_width =    side->ui->parm[ 3];
  213.     side->ui->ps_pp->page_width =        side->ui->parm[ 4];
  214.     side->ui->ps_pp->page_height =        side->ui->parm[ 5];
  215.     side->ui->ps_pp->top_margin =        side->ui->parm[ 6];
  216.     side->ui->ps_pp->bottom_margin =    side->ui->parm[ 7];
  217.     side->ui->ps_pp->left_margin =        side->ui->parm[ 8];
  218.     side->ui->ps_pp->right_margin =        side->ui->parm[ 9];
  219.     side->ui->ps_pp->terrain_gray =        side->ui->parm[10];
  220.     side->ui->ps_pp->enemy_gray =        side->ui->parm[11];
  221.     if (side->ui->print_shell) {
  222.     XtPopdown (side->ui->print_shell);
  223.     }
  224.     if (side->ui->print_help_shell) {
  225.     XtPopdown (side->ui->print_help_shell);
  226.     }
  227.     /* convert from cm or in */
  228.     if (side->ui->ps_pp->cm) {
  229.     conv = 72/2.54;
  230.     } else {
  231.     conv = 72;
  232.     }
  233.     side->ui->ps_pp->cell_size *= conv;
  234.     side->ui->ps_pp->cell_grid_width *= conv;
  235.     side->ui->ps_pp->border_width *= conv;
  236.     side->ui->ps_pp->connection_width *= conv;
  237.     side->ui->ps_pp->page_width *= conv;
  238.     side->ui->ps_pp->page_height *= conv;
  239.     side->ui->ps_pp->top_margin *= conv;
  240.     side->ui->ps_pp->bottom_margin *= conv;
  241.     side->ui->ps_pp->left_margin *= conv;
  242.     side->ui->ps_pp->right_margin *= conv;
  243.     
  244.     /* dump view */    
  245.     notify(side, "dumping view to file \"view.xconq\" ...");
  246.     flush_output(side);
  247.     dump_ps_view(side, side->ui->ps_pp, "view.xconq");
  248.     notify(side, "... done.");
  249.     
  250. }
  251.  
  252. static void 
  253. print_skip(side)
  254. Side *side;
  255. {
  256.     double conv;
  257.  
  258.     if (side->ui->print_shell)
  259.       XtPopdown (side->ui->print_shell);
  260.     if (side->ui->print_help_shell)
  261.       XtPopdown (side->ui->print_help_shell);
  262.  
  263.     /* Convert from cm or in. */
  264.     conv = 72;
  265.     if (side->ui->ps_pp->cm)
  266.       conv /= 2.54;
  267.  
  268.     side->ui->ps_pp->cell_size *= conv;
  269.     side->ui->ps_pp->cell_grid_width *= conv;
  270.     side->ui->ps_pp->border_width *= conv;
  271.     side->ui->ps_pp->connection_width *= conv;
  272.     side->ui->ps_pp->page_width *= conv;
  273.     side->ui->ps_pp->page_height *= conv;
  274.     side->ui->ps_pp->top_margin *= conv;
  275.     side->ui->ps_pp->bottom_margin *= conv;
  276.     side->ui->ps_pp->left_margin *= conv;
  277.     side->ui->ps_pp->right_margin *= conv;
  278. }
  279.  
  280. static void 
  281. update_values(side)
  282. Side *side;
  283. {
  284.     char buffer[80];
  285.     int i;
  286.     char flg;
  287.     Arg args[1];
  288.  
  289.     for (i = 0; i < N_CHOICE; i++) {
  290.     XtSetArg(args[0], XtNlabel,
  291.          (XtArgVal) choices[i].legends[side->ui->choi[i]]);
  292.     XtSetValues(side->ui->print_cmds[i + N_COMMAND], args, 1);
  293.     }
  294.     for (i = 0; i < N_TOGGLE; i++) {
  295.     flg = side->ui->flag[i];
  296.     XtSetArg(args[0], XtNstate, (XtArgVal) flg);
  297.     XtSetValues(side->ui->print_cmds[i + N_BUTTON], args, 1);
  298.     }
  299.     for (i = 0; i < N_DIALOG; i++) {
  300.     sprintf(buffer, "%.11g", side->ui->parm[i]);
  301.     XtSetArg(args[0], XtNvalue, (XtArgVal) buffer);
  302.     XtSetValues(side->ui->print_cmds[i + N_BUTTON + N_TOGGLE], args, 1);
  303.     }
  304. }
  305.  
  306. static int
  307. w_comm_ind(side, w)
  308. Side *side;
  309. Widget w;
  310. {
  311.     int i;
  312.  
  313.     for (i = 0; i <= N_WIDGET; i++) {
  314.     if (w == side->ui->print_cmds[i])
  315.       return i;
  316.     }
  317.     fprintf(stderr, "w_comm_ind: widget not found\n");
  318.     return -1;
  319. }
  320.  
  321. static void
  322. handle_do_print_dialog(w, event, params, numparms)
  323. Widget w;
  324. XEvent *event;
  325. String *params;
  326. Cardinal *numparms;
  327. {
  328.     Side *side;
  329.  
  330.     if (!find_side_via_widget(w, &side))
  331.       return;
  332.  
  333.     read_all(side);
  334.     update_values(side);
  335. }
  336.  
  337. static void 
  338. read_all(side)
  339. Side *side;
  340. {
  341.     String string;
  342.     int i;
  343.     double result;
  344.     char flg;
  345.     Arg args[1];
  346.  
  347.     for (i = 0; i < N_TOGGLE; i++) {
  348.     XtSetArg(args[0], XtNstate, (XtArgVal) &flg);
  349.     XtGetValues(side->ui->print_cmds[i + N_BUTTON], args, 1);
  350.     side->ui->flag[i] = flg;
  351.     }
  352.     for (i = 0; i < N_DIALOG; i++) {
  353.     string = XawDialogGetValueString(side->ui->print_cmds[i + N_BUTTON + N_TOGGLE]);
  354.     sscanf(string, "%lf", &result);
  355.     side->ui->parm[i] = result;
  356.     }
  357. }
  358.  
  359. static void 
  360. print_setup_button_callback(w, client_data, call_data)
  361. Widget w;
  362. XtPointer client_data;
  363. XtPointer call_data;
  364. {
  365.     int i;
  366.     Side *side;
  367.  
  368.     if (!find_side_via_widget(w, &side))
  369.       return;
  370.  
  371.     i = w_comm_ind(side, w);
  372.  
  373.     read_all(side);
  374.  
  375.     if (i == 0) {
  376.     open_print_help(side); 
  377.     } else if (i == 1) {
  378.     print_ok(side);
  379.     } else if (i == 2) {
  380.         print_skip(side);
  381.     } else if (i >= N_COMMAND) {
  382.     i -= N_COMMAND;
  383.     side->ui->choi[i] = (side->ui->choi[i] + 1) % choices[i].number;
  384.     }
  385.     update_values(side);
  386. }
  387.  
  388. /* Convert in to cm, or vice versa. */
  389.  
  390. static void 
  391. metric_convert_callback(w, client_data, call_data)
  392. Widget w;
  393. XtPointer client_data;
  394. XtPointer call_data;
  395. {
  396.     char flg;
  397.     int i;
  398.     double conv;
  399.     Arg args[1];
  400.     Side *side;
  401.  
  402.     if (!find_side_via_widget(w, &side))
  403.       return;
  404.  
  405.     XtSetArg(args[0], XtNstate, (XtArgVal) &flg);
  406.     XtGetValues(side->ui->print_cmds[side->ui->i_metric], args, 1);
  407.     conv = flg ? 2.54 : (1 / 2.54);
  408.     read_all(side);
  409.     for (i = 0; i < N_DIMEN_D; i++) {
  410.     side->ui->parm[i] *= conv;
  411.     }
  412.     update_values(side);
  413. }
  414.  
  415. static void
  416. open_print_help(side)
  417. Side *side;
  418. {
  419.     Widget w_form, w_done, w_text;
  420.  
  421.     if (!side->ui->print_help_shell) {
  422.     /* If first time, create it. */
  423.     side->ui->print_help_shell =
  424.       XtVaCreatePopupShell("PrintSetupHelp", topLevelShellWidgetClass,
  425.                    side->ui->shell,
  426.                    NULL);
  427.     w_form =
  428.       XtVaCreateManagedWidget("helpForm", formWidgetClass, side->ui->print_help_shell,
  429.                   NULL);
  430.     w_text =
  431.       XtVaCreateManagedWidget("helpText", labelWidgetClass, w_form,
  432.                   XtNlabel, helpText,
  433.                   NULL);
  434.     w_done =
  435.       XtVaCreateManagedWidget("helpDone", commandWidgetClass, w_form,
  436.                   XtNfromVert, w_text,
  437.                   NULL);
  438.     XtAddCallback(w_done, XtNcallback, done_print_help_callback, NULL);
  439.     }
  440.  
  441.     XtPopup(side->ui->print_help_shell, XtGrabNone);
  442. }
  443.  
  444. static void 
  445. done_print_help_callback(w, client_data, call_data)
  446. Widget w;
  447. XtPointer client_data;
  448. XtPointer call_data;
  449. {
  450.     Side *side;
  451.  
  452.     if (!find_side_via_widget(w, &side))
  453.       return;
  454.  
  455.     if (side->ui->print_help_shell)
  456.       XtPopdown(side->ui->print_help_shell);
  457. }
  458.  
  459.  
  460. static void
  461. handle_done_print_help(w, event, params, numparms)
  462. Widget w;
  463. XEvent *event;
  464. String *params;
  465. Cardinal *numparms;
  466. {
  467.     Side *side;
  468.  
  469.     if (!find_side_via_widget(w, &side))
  470.       return;
  471.  
  472.     if (side->ui->print_help_shell)
  473.       XtPopdown(side->ui->print_help_shell);
  474. }
  475.